﻿Imports System
Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Windows.Forms

Public Class LogManager

#Region "Write commands to log"

	Friend Shared Sub AppendLog(ByVal strMethod As String, ByVal value As String)

		AppendLog(strMethod, value, String.Empty)

	End Sub

	Friend Shared Sub AppendLog(ByVal strMethod As String, ByVal value As String, ByVal exceptionMessage As String)

		Const SEP As String = "--------------------------------------------------------------------"
		Dim objLogBuilder As New StringBuilder

		Try

			objLogBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
			objLogBuilder.Append(Environment.NewLine)

			objLogBuilder.Append(My.Resources.Messages.Method & ": ")
			objLogBuilder.Append(strMethod)
			objLogBuilder.Append(Environment.NewLine)

			objLogBuilder.Append(My.Resources.Messages.Parameters & ": ")
			objLogBuilder.Append(value)
			objLogBuilder.Append(Environment.NewLine)

			If Not String.IsNullOrEmpty(exceptionMessage) Then
				objLogBuilder.Append(My.Resources.Messages.Exception.ToUpper() & ": ")
				objLogBuilder.Append(Environment.NewLine)
				objLogBuilder.Append(exceptionMessage)
			End If

			objLogBuilder.Append(Environment.NewLine)
			objLogBuilder.Append(SEP)
			objLogBuilder.Append(Environment.NewLine)
			objLogBuilder.Append(Environment.NewLine)

			WriteToLog(objLogBuilder.ToString())

		Catch
		End Try

	End Sub

	Private Shared Sub WriteToLog(ByVal value As String)

		Dim strPath As String = Config.LogPath

		If Utils.AssemblyUtil.GetAppType = Utils.AppTypeEnum.AspNet Then
			strPath &= "\" & Path.GetFileName(HttpContext.Current.Request.Path) & ".log"
		Else
			strPath &= "\" & Path.GetFileName(Application.ExecutablePath) & ".log"
		End If

		Utils.IOUtil.WriteFile(strPath, value, True)

	End Sub

#End Region

End Class
